package com.example.springboot.service.Impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.common.ResultCode;
import com.example.springboot.common.config.JwtTokenUtils;
import com.example.springboot.dao.ActivityMapper;
import com.example.springboot.domain.po.Account;
import com.example.springboot.domain.po.Activity;
import com.example.springboot.exception.CustomException;
import com.example.springboot.service.AccountService;
import com.example.springboot.service.ActivityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.service.Impl
 * @Project：springboot
 * @name：ActivityServiceImpl
 * @Date：2025/2/23 16:55
 * @Filename：ActivityServiceImpl
 * @for:
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Resource
    private ActivityMapper activityMapper;
    
    @Override
    public List<Activity> GetAll(){
        return this.lambdaQuery().list();
    }
    @Override
    public Activity add(Activity activity) {
        String title = activity.getTitle();
        activity.setTime(DateUtil.now()); //设置当前时间为发布时间
        if (ObjectUtil.isEmpty(title)){
            throw new CustomException(ResultCode.TITLE_ISNULL);
        }
        if (ObjectUtil.isEmpty(activity.getImg())) {
            activity.setImg("http://localhost:8081/files/1738918923038-微信图片_20240629094510.jpg");
        }
        activityMapper.insert(activity);
        return activity;
    }


    @Override
    public Activity findById(Integer id) {
        return activityMapper.selectById(id);
    }




    @Override
    public void delete(Integer id) {
        activityMapper.deleteById(id);
    }


    @Override
    public PageInfo<Activity> findPage(Activity search, Integer pageNum, Integer pageSize) {
        Account user = JwtTokenUtils.getCurrentUser();
        if (ObjectUtil.isEmpty(user)) {
            throw new CustomException(ResultCode.USER_NOT_LOGIN);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Activity> all = findByCondition(search);

        return PageInfo.of(all);
    }

    @Override
    public List<Activity> findByCondition(Activity search) {
        return this.findBySearch(search);
    }



    @Override
    public Activity activityEdit(Activity activity) {

        String activityName = activity.getTitle();
        //     1.先校验标题是不是为空： 如果标题名为空，抛出异常
        if (ObjectUtil.isEmpty(activityName)){
            throw new CustomException(ResultCode.ACTIVITY_NAME_ISNULL_ERROR);
        }
        this.updateById(activity);
        return activity;
    }


    public List<Activity> findBySearch(Activity search){
            QueryWrapper<Activity> wrapper = new QueryWrapper<Activity>()
                    .like(search != null && ObjectUtil.isNotEmpty(search.getTitle()), "title", search.getTitle());
            return activityMapper.selectList(wrapper);
    }


}
